<?php

include(dirname(__FILE__).'/../../bootstrap/functional.php');

$browser = new CheckTestFunctional(
  new sfBrowser(),
  $t = new lime_test(30),
  array('doctrine' => 'sfTesterDoctrine')
);

$browser->loadData();

$browser->
  info('The permalink actions is note secured')->
  get('/')->
  click('permalink', array(), array('position' => 1))->
  with('request')->begin()->
    isParameter('module', 'todo')->
    isParameter('action', 'show')->
  end()->
  with('response')->begin()->
    isStatusCode(200)->
  end()->

  info('The todo module is secured')->
  get('/todos')->
  with('response')->begin()->
    isStatusCode(401)->
  end()->

  signin()->

  info('1 - Publications')->

  info('  1.1 - Un-Published todos are not displayed on the dashboard')->
  with('doctrine')->begin()->
    check('Todo', array(
      'title'        => 'Initially, this todo was not published',
      'is_published' => false,
    ))->
  end()->
  get('/')->
  with('response')->checkElement('#todo div.ui-widget-header:contains("not published")', false)->
  get('/todos')->

  info('  1.2 - Publish')->
  click('Publish', array(), array('position' => 1, 'method' => 'put', '_with_csrf' => true))->
  with('response')->isRedirected()->followRedirect()->
  with('doctrine')->begin()->
    check('Todo', array(
      'title'        => 'Initially, this todo was not published',
      'is_published' => true,
    ))->
  end()->
  get('/')->
  with('response')->checkElement('#todo div.ui-widget-header:contains("not published")')->
  get('/todos')->

  info('  1.3 - Un-Publish')->
  click('Unpublish', array(), array('position' => 1, 'method' => 'put', '_with_csrf' => true))->
  with('response')->isRedirected()->followRedirect()->
  with('doctrine')->begin()->
    check('Todo', array(
      'title'        => 'Customise the "Oops! Page Not Found" page',
      'is_published' => false,
    ))->
  end()->
  click('Publish', array(), array('position' => 1, 'method' => 'put', '_with_csrf' => true))->
  with('response')->isRedirected()->followRedirect()->


  info('2 - Order the  Todo display')->

  info('  2.1 - Up')->
  get('/')->
  with('response')->checkElement('#todo .todo:first div.ui-widget-header:contains("Oops! Page Not Found")')->
  get('/todos')->
  click('Promote', array(), array('position' => 2, 'method' => 'put', '_with_csrf' => true))->
  with('response')->isRedirected()->followRedirect()->
  get('/')->
  with('response')->checkElement('#todo .todo:first div.ui-widget-header:contains("Oops! An Error Occurred")')->
  get('/todos')->


  info('  2.1 - Down')->
  get('/')->
  with('response')->checkElement('#todo .todo:first div.ui-widget-header:contains("Oops! An Error Occurred")')->
  get('/todos')->
  click('Demote', array(), array('position' => 1, 'method' => 'put', '_with_csrf' => true))->
  with('response')->isRedirected()->followRedirect()->
  get('/')->
  with('response')->checkElement('#todo .todo:first div.ui-widget-header:contains("Oops! Page Not Found")')->
  get('/todos')->

  info('3 - Todo Creation')->
  click('New')->
  info('  3.1 - The preview diplayes the html version on the todo')->
  click(
    'Preview',
    array(
      'todo' => array(
        'title'   => 'Write your tests',
        'content' => 'Tests are great.',
      )
    )
  )->
  with('response')->checkElement('#sf_admin_header .preview')->
  info('  3.2 - The preview is diplayed only when the form is valid')->
  click(
    'Preview',
    array(
      'todo' => array(
        'title'   => '',
        'content' => '',
      )
    )
  )->
  with('form')->begin()->
    hasErrors(2)->
    isError('title',   'required')->
    isError('content', 'required')->
  end()->
  click(
    'Save',
    array(
      'todo' => array(
        'title'   => 'Write your tests',
        'content' => 'Tests are great.',
      )
    )
  )->
  with('form')->begin()->
    hasErrors(false)->
  end()->
  info('  3.3 - By default, new todos are published and appear in the bottom off the dashboard')->
  get('/')->
  with('response')->checkElement('#todo .todo:last div.ui-widget-header:contains("Write your tests")')->
  get('/todos')->
  info('  3.4 - Adding tags')->
  click('Write your tests')
  ;
$securityTag = Doctrine::getTable('Tag')->findOneByName('security');
$browser->
  select('todo_tags_list_'.$securityTag->id)->
  click('Save')->
  with('form')->begin()->
    hasErrors(false)->
  end()->
  get('/')->
  with('response')->checkElement('#todo .todo:last div.ui-widget-header .tags:contains("security")')->
  get('/todos')->

  info('  3.4 - Removing tags')->
  click('Write your tests')->
  deSelect('todo_tags_list_'.$securityTag->id)->
  click('Save')->
  with('form')->begin()->
    hasErrors(false)->
  end()->
  get('/')->
  with('response')->checkElement('#todo .todo:last div.ui-widget-header .tags:contains("security")', false)
  ;
